import Vue from 'vue'
import Vuex from 'vuex'

import {
	tag,
	service,
	giftList,
	messageCount,
	area,
	getUserInfo,
	bindParent,
	configData,
	getarea
} from '@/common/api.js'

import {
	baseUrl
} from '@/common/config.js'

Vue.use(Vuex) // vue的插件机制

// Vuex.Store 构造器选项
const store = new Vuex.Store({
	// 为了不和页面或组件的data中的造成混淆，state中的变量前面建议加上$符号
	state: {
		badge: 0,
		imgData: {
			homeBg: baseUrl + '/assets/web/home/bg.png',
			packageBg: baseUrl + '/assets/web/home/package-bg.png',
			clockBg: baseUrl + '/assets/web/clock/bg.png',
			messageBg: baseUrl + '/assets/web/message/bg.png',
			settleBg: baseUrl + '/assets/web/settle/bg.png',
			mineBg: baseUrl + '/assets/web/mine/bg.png',
			shareBg: baseUrl + '/assets/web/share/bg.png',

			settleBg: baseUrl + '/assets/web/settle/apply-bg.png',

			settleDesc: baseUrl + '/assets/web/settle/settle-desc.png',
			vipImg: baseUrl + '/assets/web/mine/vip-img.png',
			staffImg: baseUrl + '/assets/web/mine/staff-img.png',
			rechargeBg: baseUrl + '/assets/web/mine/recharge-bg.png',
			releaseBg: baseUrl + '/assets/web/settle/release-bg.png',
			vipBg: baseUrl + '/assets/web/mine/vip-bg.png',
			settleHeader: baseUrl + '/assets/web/settle/header.png',
			clock: baseUrl + '/assets/web/settle/clock.png',
			clockStaff: baseUrl + '/assets/web/settle/clock-staff.png',
			vipStaff: baseUrl + '/assets/web/settle/vip-staff.png',
			apply1: baseUrl + '/assets/web/settle/apply-1.png',
			apply2: baseUrl + '/assets/web/settle/apply-2.png',
			apply3: baseUrl + '/assets/web/settle/apply-3.png',
			applys1: baseUrl + '/assets/web/settle/applys-2.png',
			applys2: baseUrl + '/assets/web/settle/applys-2.png',
			applys3: baseUrl + '/assets/web/settle/applys-3.png',
			wishBg: baseUrl + '/assets/web/mine/wish-bg.png',
			logoBg: baseUrl + '/assets/web/home/logo-bg.png',
			playingBg: baseUrl + '/assets/web/home/playing-bg.png',
			playing1: baseUrl + '/assets/web/home/playing-1.png',
			vipBg1: baseUrl + '/assets/web/settle/vip-bg.png',
			vipLogo: baseUrl + '/assets/web/settle/vip-logo.png',
			vipHeader: baseUrl + '/assets/web/settle/vip-header.png',
			vipBottom: baseUrl + '/assets/web/settle/vip-bottom.png',
		},
		systemInfo: null,
		cityInfo: null,
		// 用户信息
		$userInfo: uni.getStorageSync('userInfo') ? uni.getStorageSync('userInfo') : {},
		// token: "08eb1658-8ec4-4eab-8ab8-f23359ac4929",

		token: uni.getStorageSync('token') ? uni.getStorageSync('token') : null,
		isLogin: uni.getStorageSync('token') ? true : false,
		p_id: 0,

		configData: {},
		uploadData: {},
		areaData: [],
		tagData: [],
		serveData: [],
		giftData: [],
		cityName: null,
		city: 0
	},
	mutations: {
		setSystemInfo(state, payload) {
			state.systemInfo = payload;
		},
		setUserInfo: (state, data) => {
			uni.setStorageSync('userInfo', data);
			uni.setStorageSync('token', data.token);

			state.$userInfo = uni.getStorageSync('userInfo');
			state.token = uni.getStorageSync('token');
			state.isLogin = true;
		},
		setUserLogout: (state, data) => {
			uni.removeStorageSync('userInfo');
			uni.removeStorageSync('token');

			state.isLogin = false;
		},
		setConfigInfo(state, data) {
			state.configData = data;
		},
		setMessageCount(state, data) {
			state.badge = data
		},
		setTagInfo(state, data) {
			state.tagData = data;
			state.tagData.unshift({
				'id': 0,
				'name': '全部'
			})
		},
		setServeData(state, data) {
			state.serveData = data;
		},
		setGiftData(state, data) {
			state.giftData = data;
		},
		setCityInfo(state, data) {
			console.log('datadata', data);
			state.cityName = data.city
			state.cityInfo = data
		},
		setCityId(state, data) {
			state.city = data
		},
		setCityName(state, data) {
			state.cityName = data
		}
	},
	actions: {
		setUserLogout(context) {
			context.commit('setUserLogout')
		},
		setBindParent(context, data) {
			console.log('bindParentbindParentbindParent', data);
			bindParent({
				custom: {
					toast: false
				},
				data: {
					'p_id': data.user_id
				}
			}).then(res => {
				console.log('res', res);
			})
		},
		getUserInfo(context) {
			getUserInfo().then(res => {
				context.commit('setUserInfo', res.data)
			}).catch(err => {
				return Promise.reject(err)
			})
		},
		getConfigInfo(context) {
			configData().then(res => {
				console.log('init', res);
				context.commit('setConfigInfo', res)
			}).catch(err => {
				console.log(err);
			})
		},
		getTagInfo(context) {
			tag().then(res => {
				console.log('init', res);
				context.commit('setTagInfo', res)
			}).catch(err => {
				console.log(err);
			})
		},
		getGiftData(context) {
			giftList().then(res => {
				console.log('init', res);
				context.commit('setGiftData', res)
			}).catch(err => {
				console.log(err);
			})
		},

		getServeData(context) {
			service().then(res => {
				context.commit('setServeData', res)
			}).catch(err => {
				console.log(err);
			})
		},
		getAreaInfo(context) {
			console.log('context',context.state.cityName);
			getarea({
				data: {
					city: context.state.cityName
				},
				custom: {
					catch: true
				}
			}).then(res => {
				context.commit('setCityId', res.id)
				context.commit('setCityName', res.name)
			}).catch(err => {
				console.log(err);

			})
		},
		getMessageCount(context) {
			messageCount({
				custom: {
					catch: true
				}
			}).then(res => {
				console.log('badge', res);
				context.commit('setMessageCount', res.count)
			}).catch(err => {
				console.log(err);
				context.commit('setMessageCount', 0)
			})
		},
		getAreaData({
			state,
			commit
		}) {
			area().then(res => {
				state.areaData[0] = res;
				area({
					data: {
						'province': 1
					}
				}).then(res => {
					console.log('北京市', res);
					state.areaData[1] = res;
					area({
						data: {
							'province': 1,
							'city': 2
						}
					}).then(res => {
						console.log('北京市111', res);
						state.areaData[2] = res;
					})
				})
			})
		}
	}
})

export default store